package com.umeox.calculator;

import android.content.Context;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class Persist {
    History history = new History();
    private Context mContext;
    private int mDeleteMode;

    Persist(Context context) {
        this.mContext = context;
    }

    public void setDeleteMode(int mode) {
        this.mDeleteMode = mode;
    }

    public int getDeleteMode() {
        return this.mDeleteMode;
    }

    public void load() {
        try {
            DataInputStream in = new DataInputStream(new BufferedInputStream(this.mContext.openFileInput("calculator.data"), 8192));
            int version = in.readInt();
            if (version > 1) {
                this.mDeleteMode = in.readInt();
            } else if (version > 2) {
                throw new IOException("data version " + version + "; expected " + 2);
            }
            this.history = new History(version, in);
            in.close();
        } catch (FileNotFoundException e) {
            Calculator.log("" + e);
        } catch (IOException e2) {
            Calculator.log("" + e2);
        }
    }

    public void save() {
        try {
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(this.mContext.openFileOutput("calculator.data", 0), 8192));
            out.writeInt(2);
            out.writeInt(this.mDeleteMode);
            this.history.write(out);
            out.close();
        } catch (IOException e) {
            Calculator.log("" + e);
        }
    }
}
